package org.smr.ministore.controller;

import javax.annotation.Resource;

import org.apache.commons.collections.MapUtils;
import org.blade.msg.DataMessage;
import org.blade.msg.ErrorCode;
import org.blade.msg.Message;
import org.blade.utils.Pager;
import org.smr.ministore.entities.GoodsPrice;
import org.smr.ministore.service.GoodsPriceService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
import java.util.Map;
import java.util.Objects;


@Controller("GoodsPriceController")
public class GoodsPriceController {

	@Resource
	private GoodsPriceService service;
	
	/**
	 * 保存编辑后的GoodsPrice信息
	 * @param goodsPrice
	 * @return
	 */
	@ResponseBody
	@RequestMapping(method = RequestMethod.PATCH, value = "/GoodsPrice/edit")
	public Message modify(@RequestBody GoodsPrice goodsPrice){
		Message msg = new Message();
		Long tenantId = goodsPrice.getTenantId();
		if(Objects.isNull(tenantId) ){
			msg.setStatus(ErrorCode.Business.Saas.Invalid_tenantId);
			return msg;
		}

		if(null == goodsPrice.getId()){
			msg.setStatus(service.save(goodsPrice));
		}else{
			msg.setStatus(service.updateById(goodsPrice));
		}
		return msg;
	}
	
	/**
	* 跳转到列表查询GoodsPrice
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/GoodsPrice/list-page")
	public String toList(){
		return "/GoodsPrice/GoodsPrice-list";
	}

	/**
	*  根据id查询GoodsPrice
	* @return
	*/
	@ResponseBody
	@RequestMapping(method = RequestMethod.GET, value = "/GoodsPrice")
	public GoodsPrice get( Long id ){
		return service.queryById(id);
	}


    /**
	 * 分页查询GoodsPrice信息
	 * @param GoodsPrice
	 * @param pageSize
	 * @param pageNum
	 * @return
	 */
	@ResponseBody
	@RequestMapping( value = "/GoodsPrice/list")
	public Pager<GoodsPrice> list(@RequestBody GoodsPrice GoodsPrice , long pageSize, long pageNum ){

		return service.paginate(GoodsPrice, pageSize, pageNum);

	}
	
	
	/**
	 * 根据ID删除GoodsPrice信息
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping( value = "/GoodsPrice/remove",method = RequestMethod.DELETE)
	public Message remove( Long id){
		
		Message msg = new Message();
		msg.setStatus(service.deleteById(id));
		return msg;
	}


    @ResponseBody
    @RequestMapping( method = RequestMethod.GET, value = "/GoodsPrice/getGoodsPriceList")
    public  Pager<Map<String, Object>> getGoodsPriceList(Long organId, Long typeId,
                                                       Long brandId, String goodsName,
                                                       Integer quote,
                                                       Long pageSize, Long pageNum) {


        Pager<Map<String, Object>> pager = this.service.getGoodsPriceList( organId,
                                                                                    typeId,
                                                                                    brandId,
                                                                                    goodsName,
                                                                                       quote,
                                                                                    pageSize,
                                                                                    pageNum);

        List<Map<String, Object>> goodsPriceList = pager.getEntities();
        for(Map goodsPrice : goodsPriceList){

            String id = MapUtils.getString(goodsPrice,"id");
            goodsPrice.put("id", id);
            String supplierId = MapUtils.getString(goodsPrice,"supplierId");
            goodsPrice.put("supplierId", supplierId);
            String goodsId = MapUtils.getString(goodsPrice,"goodsId");
            goodsPrice.put("goodsId", goodsId);
            String organIdStr = MapUtils.getString(goodsPrice,"organId");
            goodsPrice.put("organId", organIdStr);
        }

        return pager;
    }

    @ResponseBody
    @RequestMapping( method = RequestMethod.POST, value = "/GoodsPrice/getGoodsPriceList/{pageSize}/{pageNum}")
    public  Pager<Map<String, Object>>  getGoodsPriceList(@RequestBody Map<String, Object> params,
                                                       @PathVariable("pageSize") Long pageSize,
                                                       @PathVariable("pageNum") Long pageNum) {

        Long organId = MapUtils.getLong(params,"organId");
        Long typeId  = MapUtils.getLong(params,"typeId");
        Long brandId  = MapUtils.getLong(params,"brandId");
        String goodsName  = MapUtils.getString(params,"goodsName");
        Integer quote  = MapUtils.getIntValue(params,"quote", 0);
        Pager<Map<String, Object>> pager = this.service.getGoodsPriceList( organId,
                typeId,
                brandId,
                goodsName,
                quote,
                pageSize,
                pageNum);

        List<Map<String, Object>> goodsPriceList = pager.getEntities();
        for(Map goodsPrice : goodsPriceList){

            String id = MapUtils.getString(goodsPrice,"id");
            goodsPrice.put("id", id);
            String supplierId = MapUtils.getString(goodsPrice,"supplierId");
            goodsPrice.put("supplierId", supplierId);
            String goodsId = MapUtils.getString(goodsPrice,"goodsId");
            goodsPrice.put("goodsId", goodsId);
            String organIdStr = MapUtils.getString(goodsPrice,"organId");
            goodsPrice.put("organId", organIdStr);
        }

        return pager;
    }


    @ResponseBody
    @RequestMapping( method = RequestMethod.POST, value = "/GoodsPrice/getGoodsPriceBy/{organId}/{goodsId}")
    public Message getGoodsPriceBy(@PathVariable("organId")Long organId,
                                      @PathVariable("goodsId")Long goodsId){

	    GoodsPrice goodsPrice = this.service.getGoodsPriceBy(organId, goodsId);
	    Message message = new DataMessage(ErrorCode.Fail.FAIL,"", goodsPrice);
        return message;
    }
}
